package cn.swust.designpatternlearningplatform.common.utils;


import cn.swust.designpatternlearningplatform.domain.bo.OptionsBo;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPersonalTrainingResultDto;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**

  * @return null
 * @author wang
 * description 生成word文档
 * date 2023/6/19 19:37
 */
public class WordUtils {
    @Value("${file_path}")
    private String filePath;
    public File getWord (List<StudentPersonalTrainingResultDto> list1, String studentNumber, String studentName,String paperNumber) throws Exception {
        // 创建一个新的 Word 文档
        XWPFDocument document = new XWPFDocument();

        // 添加标题，题目命名没成功
        XWPFParagraph title = document.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = title.createRun();
        titleRun.setText(studentName);
        titleRun.setBold(true);
        titleRun.setFontSize(20);
        // 添加类数据
        for (int i = 0; i < list1.size(); i++) {
            StudentPersonalTrainingResultDto s=list1.get(i);
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText(s.getTopicContent());
            run.addCarriageReturn();
            if (s.getTopicContentPath()!=null){
                File file=new File(s.getTopicContentPath());
                if(file.exists())
                {
                    FileInputStream fis=new FileInputStream(file);
                    run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "example", Units.toEMU(200), Units.toEMU(200));
                    run.addCarriageReturn();
                }
            }
            List<OptionsBo> optional=s.getOptions();
            List<OptionsBo> sorted = optional.stream().sorted(Comparator.comparing(OptionsBo::getOptionName)).collect(Collectors.toList());
            for (int j = 0; j < sorted.size(); j++) {
                run.setText(sorted.get(j).getOptionName()+sorted.get(j).getOptionContent());
                run.addCarriageReturn();
                if (sorted.get(j).getOptionContentPath()!=null){
                    File file=new File(sorted.get(j).getOptionContentPath());
                    if(file.exists())
                    {
                        FileInputStream fis=new FileInputStream(file);
                        run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "example", Units.toEMU(200), Units.toEMU(200));
                        run.addCarriageReturn();
                    }
                }
            }
            run.setText("我的答案："+s.getOwnAnswer());
            run.addCarriageReturn();
            run.setText("正确得答案是："+s.getRightAnswer());
            run.addCarriageReturn();
            run.setText("解析："+s.getTopicAnalyse());
            run.addCarriageReturn();
            if (s.getTopicAnalysePath()!=null){
                File file=new File(sorted.get(i).getOptionContentPath());
                if(file.exists())
                {
                    FileInputStream fis=new FileInputStream(file);
                    run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "example", Units.toEMU(200), Units.toEMU(200));
                }
            }
        }
        // 保存 Word 文档
        FileOutputStream out = new FileOutputStream(studentNumber+".doc");
        document.write(out);
        out.close();
        document.close();
        File file=new File(studentNumber+".doc");
        return file;
    }


}
